
package com.zsrt.book.dao.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.zsrt.book.dao.BookManagerDao;
import com.zsrt.book.entity.BookEntity;
import com.zsrt.book.util.BookUtil;
import com.zsrt.book.util.CommonUtil;

public class BookManagerDaoImpl implements BookManagerDao{

	List<BookEntity> bookLists = BookUtil.init();
	/**
	 * 增加一个新的信息
	 */
	@Override
	public boolean addBook(Scanner scanner,String bookConstants) {
		//读出文件
		boolean b =true;
		String file = bookConstants;
		SAXReader reader = new SAXReader();
		Document  document =null;
		try {
			document = reader.read(file);
			Element root = document.getRootElement();
			
			System.err.println("请输入您想添加的图书编号");
			int id = scanner.nextInt();
			System.err.println("请输入您想添加的图书名称");
			String name = scanner.next();
			System.err.println("请输入您想添加的图书简介");
			String intro = scanner.next();
			System.err.println("请输入您想添加的图书借阅状态      Y:可以借阅   N:已经借出 ");
			String state = scanner.next();
			//得到所有的book节点
			List<Element> nodes = root.selectNodes("//book");
			//遍历节点 看图书编号和图书名称是否已存在
			for(Element book:nodes){
				int i=Integer.parseInt(book.element("bookNum").getText());
				String n =book.element("bookName").getText();
				//两次验证是 或 的关系
				if( i==id || n.equals(name)){
					b = false;
					System.err.println("图书编号或名字已存在！");
					return b;
				}
			}
			//添加新的图书	
			System.err.println("添加成功");
			Element bookElement = root.addElement("book");
			Element numElement = bookElement.addElement("bookNum");
			numElement.setText(""+id);
			Element nameElement = bookElement.addElement("bookName");
			nameElement.setText(name);
			Element descriptionElement = bookElement.addElement("description");
			descriptionElement.setText(intro);
			Element borrowStateElement = bookElement.addElement("borrowState");
			borrowStateElement.setText(state);
			
			BookEntity bookEntity = new BookEntity();
			bookEntity.setBookNum(id);
			bookEntity.setBookName(name);
			bookEntity.setDescription(intro);
			bookEntity.setBorrowState(state);
			bookLists.add(bookEntity);
			
			//打印新添加的信息
			System.err.println(bookEntity);
			try {
				CommonUtil.add(document, file);
			} catch (Exception e) {
				e.printStackTrace();
			}
				
			
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		return b;
	}

	/**
	 * 修改图书信息
	 */
	@Override
	public boolean updateBook(Scanner scanner, int id,String bookConstants) {
			
		String file = bookConstants;
		SAXReader reader = new SAXReader();
		Document  document =null;
		try {
			document = reader.read(file);
			Element root = document.getRootElement();
			
			//得到所有节点
			List<Element> nodes = root.selectNodes("//book");
			//遍历节点查询id是否存在
			for(Element book:nodes){
				if(id == Integer.parseInt(book.element("bookNum").getText())){
					System.err.println("请输入修改后的图书名字");
					String name = scanner.next();
					// 如果存在   遍历节点查询名称是否存在
					for(Element e:nodes){
						if(name.equals(e.element("bookName").getText())){
							System.err.println("该图书已存在");
							return false;
						}
					}	
					//id存在且 名称不存在则添加新的图书名称
					Element nameElement = book.element("bookName");
					nameElement.setText(name);

					//更改图书数组
					for(BookEntity bookEntity:bookLists){
						if(id == bookEntity.getBookNum()){
							bookEntity.setBookName(name);
							//打印新添加的图书数组
							System.out.println(bookEntity);
						}
					}
					
					try {
						//写回文件
						CommonUtil.add(document, file);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
					return true;
				}
				
			}
			//编号不存在
			System.err.println("该图书编号不存在");
			return false;
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 删除节点
	 */
	@Override
	public boolean deleteBook(int id, String bookConstants) {
		
		//更改图书数组
		for(int index=0;index<bookLists.size();index++){
			BookEntity bookEntity = bookLists.get(index);
			if(id == bookEntity.getBookNum()){
				bookLists.remove(index);
			}
			//System.err.println(bookEntity);
		}
		
		//得到文件
		String file = bookConstants;
		SAXReader reader = new SAXReader();
		Document  document =null;
		File f = new File(file);
		try {
			document = reader.read(f );
			Element root = document.getRootElement();
			
			//得到所有节点
			List<Element> nodes = root.selectNodes("//book");
			//遍历节点看图书编号是否存在
			for(Element node:nodes){
				//如果存在就删除
				if(id==Integer.parseInt(node.element("bookNum").getText())){
					node.getParent().remove(node);
					
					try {
						CommonUtil.add(document, file);
					} catch (Exception e) {
						e.printStackTrace();
					}
					return true;
				}
			}
			//图书编号不存在
			System.err.println("该图书编号不存在");
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		return false;
	}

	/**
	 * 显示所有图书信息
	 */
	@Override
	public void showBooks(String bookConstants) {

		//读出文件
		String file = bookConstants;
		SAXReader reader = new SAXReader();
		Document  document =null;
		File f = new File(file);
		try {
			document = reader.read(f );
			Element root = document.getRootElement();
			//List<Element> nodes = root.selectNodes("//book");
			List<BookEntity> bookLists = new ArrayList<BookEntity>();
			
			//遍历所有节点
			for(Iterator it=root.elementIterator();it.hasNext();){
				
				Element element = (Element) it.next();        
				// 得到每个子节点  
				Element stu = element.element("bookNum");
				Element stuNum = element.element("bookName");
				Element stuAge = element.element("description");
				Element stuSex = element.element("borrowState");
				
				//得到一个节点的所有属性
				BookEntity book01 = new BookEntity(Integer.parseInt(stu.getText()), stuNum.getText(), stuAge.getText(), stuSex.getText());
				bookLists.add(book01);
			}   
			//打印的是图书字符串
			for(BookEntity bookEntity:bookLists){
				System.err.println(bookEntity);
			}
		} catch (DocumentException e) {
			
			e.printStackTrace();
		}
		
		//得到的是xml文档格式
		/*String file = bookConstants;
		SAXReader reader = new SAXReader();
		Document  document =null;
		File f = new File(file);
		try {
			document = reader.read(f );
			Element root = document.getRootElement();
			try {
				CommonUtil.print(document);
				CommonUtil.getOutputFormat();
				CommonUtil.add(document, file);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}*/
	}
	
	
}
